package yunhe.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 权限过滤
 * Created by lzh on 2017/6/20.
 */

/**
 * 没登陆不允许访问这些内容
 */
@WebFilter(filterName = "AccessFilter",urlPatterns = {"/stockmgr/*","/sale/*","/stock/*","/purchase/*",
        "/index.jsp","/welcome.html","/FileMgr/*","/receipt/*",})
public class AccessFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpSession session=((HttpServletRequest)(req)).getSession();
        Object user=session.getAttribute("user");
        if(user!=null&&!user.equals("false")){
            chain.doFilter(req, resp);
        }
        else{
            ((HttpServletResponse)(resp)).sendRedirect("/login.jsp");
        }

    }

    public void init(FilterConfig config) throws ServletException {

    }

}
